﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IVisa
    {
        public DataSet Visa_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Visa_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Visa_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public VisaInfo Visa_GetInfo(int VisaID)
        {
            VisaInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", VisaID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new VisaInfo();
                    retVal.ID = Convert.ToInt32(dr["Visa_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Visa_EmployeeID"]);
                    retVal.No = dr["Visa_No"].ToString();
                    if (dr["Visa_IssueDate"] != DBNull.Value)
                        retVal.IssueDate = Convert.ToDateTime(dr["Visa_IssueDate"]);
                    if (dr["Visa_ExpireDate"] != DBNull.Value)
                        retVal.ExpireDate = Convert.ToDateTime(dr["Visa_ExpireDate"]);
                    retVal.AuthorIssue = dr["Visa_AuthorIssue"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Visa_Insert(VisaInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Visa_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Visa_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@Visa_IssueDate", eInfo.IssueDate);
            dbCmd.Parameters.AddWithValue("@Visa_ExpireDate", eInfo.ExpireDate);
            dbCmd.Parameters.AddWithValue("@Visa_AuthorIssue", eInfo.AuthorIssue);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Visa_Update(VisaInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Visa_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Visa_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Visa_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@Visa_IssueDate", eInfo.IssueDate);
            dbCmd.Parameters.AddWithValue("@Visa_ExpireDate", eInfo.ExpireDate);
            dbCmd.Parameters.AddWithValue("@Visa_AuthorIssue", eInfo.AuthorIssue);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Visa_Delete(int VisaID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Visa_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", VisaID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

